package com.liveinstars.im.domain.manager.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.api.im.query.UserOnlineServerQuery;
import com.liveinstars.im.domain.dao.mapper.UserOnlineServerMapper;
import com.liveinstars.im.domain.dao.model.UserOnlineServer;
import com.liveinstars.im.domain.manager.UserOnlineServerManager;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户关联server表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-31
 */
@Service
public class UserOnlineServerManagerImpl extends ServiceImpl<UserOnlineServerMapper, UserOnlineServer> implements UserOnlineServerManager {

    private Logger logger = LoggerFactory.getLogger(UserOnlineServerManagerImpl.class);

    @Override
    public int countByQuery(UserOnlineServerQuery query) {
        try {
            QueryWrapper<UserOnlineServer> queryWrapper = new QueryWrapper<>();
            if (query.getChatServerId() != null) {
                queryWrapper.eq("chat_server_id", query.getChatServerId());
            }
            if (query.getOnlineStatus() != null) {
                queryWrapper.eq("online_status", query.getOnlineStatus());
            }
            if (query.getUserAuthTokenId() != null) {
                queryWrapper.eq("user_authtoken_id", query.getUserAuthTokenId());
            }
            int count = this.count(queryWrapper);
            return count;
        } catch (Exception e) {
            logger.error("countByQuery params:{} errorMsg:{}",
                    "query="+ JSON.toJSONString(query), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }

    }

    @Override
    public void add(UserOnlineServer userOnlineServer) {
        try {
            this.save(userOnlineServer);
        } catch (Exception e) {
            logger.error("add params:{} errorMsg:{}",
                    "userOnlineServer="+ JSON.toJSONString(userOnlineServer), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
